package com.demo.search.spring.xmlparser;

import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;
import org.xml.sax.SAXException;

import com.demo.common.ClassLoaderUtils;
import com.demo.search.xmlparser.QueryTemplateManagerWrapper;

/**
 * Query模板管理器封装对象
 * <p>封装QueryTemplateManager,并遍历xsl文件夹,加载模板</p>
 * @author ryuu.kk
 *
 */
public class SpringQueryTemplateManagerWrapper extends QueryTemplateManagerWrapper implements ServletContextAware, InitializingBean {

	/**
	 * Servlet上下文容器
	 */
	private ServletContext servletContext;

	/**
	 * 构造函数,创建根
	 * @param xslIs
	 * @throws TransformerConfigurationException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public SpringQueryTemplateManagerWrapper(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException {
		super(xslIs);
	}

	/**
	 * 构造函数
	 * @param xslPath
	 * @param filter
	 * @throws FileNotFoundException
	 */
	public SpringQueryTemplateManagerWrapper(String xslPath, FileFilter filter)
			throws FileNotFoundException {
		super(xslPath, filter);
	}

	/**
	 * 获取web-app的真实路径
	 * @see com.demo.search.xmlparser.QueryTemplateManagerWrapper#getRealPath(java.lang.String)
	 */
	@Override
	public String getRealPath(String path) {
		path = ClassLoaderUtils.getRealPath(path);
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		return servletContext.getRealPath(path);
	}
	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		super.queryTemplateLoader();
	}

}
